<?php

namespace app\admin\model;

use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Db;
use think\Model;

/**
 * XCertificate
 * @controllerUrl 'xCertificate'
 */
class XCertificate extends Model
{
    // 表主键
    protected $pk = 'ID';
    
    // 表名
    protected $table = 'x_certificate';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    protected $createTime = false;
    protected $updateTime = false;



    public function getDemoAttr($value, $row)
    {
        return !$value ? '' : $value;
    }
    /**
     * 添加
     */
    public function add()
    {


        if ($this->request->isPost()) {

            $data = $this->request->post();
            if (!$data) {
                $this->error(__('Parameter %s can not be empty', ['']));
            }

            $data = $this->excludeFields($data);
            if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                $data[$this->dataLimitField] = $this->auth->id;
            }
            $adminInfo          = $this->auth->getInfo();
            $data['ID']='ZJ'.$this->microtime_format('YmdHisx',$this->microtime_float());
            $data['OPNAME']=$adminInfo['nickname'];
            $data['OPDATETIME']=date('Y-m-d H:i:s',time());
            $result = false;
            Db::startTrans();
            try {
                // 模型验证
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate;
                        if ($this->modelSceneValidate) $validate->scene('add');
                        $validate->check($data);
                    }
                }
                $result = $this->model->save($data);
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(__('Added successfully'));
            } else {
                $this->error(__('No rows were added'));
            }
        }
        $this->error(__('Parameter error'));
    }
}